package com.example.rg1musicplayerapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private Button buttonNext;
    private ProgressBar progressBar;

    //服务绑定
    private PlayMusicService.PlayMusicBinder playMusicBinder;
    //连接对象
    private PlayServiceConnection playServiceConnection;

    private boolean isStart=true;
    private boolean isPause=false;
    private Timer timer;
    private TimerTask timerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        buttonNext=findViewById(R.id.buttonNext);
        progressBar=findViewById(R.id.progressBar);
        //指明哪个界面绑定哪个服务
        Intent playMusicIntent=new Intent(MainActivity.this,PlayMusicService.class);
        playServiceConnection=new PlayServiceConnection();
        bindService(playMusicIntent,playServiceConnection,BIND_AUTO_CREATE);
        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,DetailActivity.class);
                startActivity(intent);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isStart)
                {
                    playMusicBinder.startPlay();
                    isStart=false;
                    //计时器
                    timer=new Timer();
                    timerTask=new TimerTask() {
                        @Override
                        public void run() {
                            Log.d("music",playMusicBinder.getProgress()+"");
                            progressBar.setProgress(playMusicBinder.getProgress());
                        }
                    };
                    timer.schedule(timerTask,0,10);
                    return;
                }
                if(isPause)
                {
                    playMusicBinder.PauseorContinue();
                    isPause=false;
                    button.setBackgroundResource(R.drawable.play);
                }
                else
                {
                    playMusicBinder.PauseorContinue();
                    isPause=true;
                    button.setBackgroundResource(R.drawable.pause);
                }
            }
        });
    }
    //服务连接
    class PlayServiceConnection implements ServiceConnection
    {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            playMusicBinder=(PlayMusicService.PlayMusicBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}
